package com.biz.primus.ms.commodity.service;

import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.commodity.dao.po.Category;
import com.biz.primus.ms.commodity.dao.redis.CategoryRedisDao;
import com.biz.primus.ms.commodity.dao.repository.category.CategoryRepository;
import com.biz.primus.ms.commodity.trans.Category2CategoryRo;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 分类抽象Service
 *
 * @author david-liu
 * @date 2017年01月24日
 * @reviewer
 */
public class AbstractCategoryService extends AbstractBaseService {

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private CategoryRedisDao categoryRedisDao;

    protected void saveCategory(Category category) {
        this.saveOrUpdateUsingPo(categoryRepository, categoryRedisDao, category, new Category2CategoryRo());
        //通过渠道控制是否显示在前端
        if (category.getSaleChannel() == null){
            return;
        }
        if (category.getSaleChannel() == SaleChannel.ALL || category.getSaleChannel() == SaleChannel.APP){
            this.delayer.executeAfterTransactionCommit(()->categoryRedisDao.setAppCategorySort(category.getId(),
                    Double.valueOf(ValueUtils.getValue(category.getIdx()))));
        }else {
            this.delayer.executeAfterTransactionCommit(()->categoryRedisDao.removeCategoryFromAppSort(category.getId()));
        }
    }
}
